package server;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;



@SuppressWarnings("serial")
public class SiteImpl extends UnicastRemoteObject implements SiteItf ,Runnable {

	private ArrayList<SiteItf> children;
	private String name;
	private SiteItf father;

	
	/**
	 * @category Constructor
	 * @param name
	 * @throws RemoteException
	 */
	public SiteImpl(String name) throws RemoteException{
		super();
		this.name = name;
	}
	

	/**
	 * @param message type <b>byte[]</b>
	 */
	@Override
	public boolean tellChildren(byte[] message) {
		verbose(new String(message)); 
		
		if (children.size() == 0){
			return true;
		}else{
			for (SiteItf child : children){
				try {
					child.tellChildren(message);
				} catch (RemoteException e) {e.printStackTrace();}
			}
			return true;
		}
	}

	/**
	 * @param message type <b>String</b>
	 */
	@Override
	public void verbose(String message) {
		System.out.println(name + " says>"+ message);
		
	}

	/**
	 * @return Boolean
	 */
	@Override
	public boolean isRoot() {
		if (father == null){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * @return boolean
	 * @param child type <b>SiteItf</b>
	 */
	@Override
	public boolean addChild(SiteItf child) {
		boolean childAdded = true;
		try {
			child.setFather(this);
			children.add(child);
		} catch (RemoteException e) {
			childAdded = false;
			e.printStackTrace();
		}
		if(!childAdded){
			try {
				System.out.println(child.getNodeName() +" can t be son of " + getNodeName());
			} catch (RemoteException e) {e.printStackTrace();}
		}
		return childAdded;
	}
	
	/**
	 * @return String
	 */
	@Override
	public String getNodeName(){
		return name;
	}
	

	@Override
	public boolean createChild(String name) throws RemoteException {
		boolean added = true;
		SiteImpl node = new SiteImpl(name);
		try {
			Naming.bind(name, node);
		} catch (MalformedURLException e) {
			added = false;
			//e.printStackTrace();
		} catch (AlreadyBoundException e) {
			added = false;
			//e.printStackTrace();
		}
		if (!added)System.out.println(name + " not created");
		return false;
	}


	@Override
	public boolean remove() throws RemoteException {
		boolean removed = true;
		try {
			for (SiteItf removeTree : children)
				removeTree.remove();
			Naming.unbind(name);
		} catch (MalformedURLException e) {
			removed = false;
			e.printStackTrace();
		} catch (NotBoundException e) {
			removed = false;
			e.printStackTrace();
		}
		if(!removed) System.out.println(name + " not removed");
		return false;
	}


	@Override
	public void setFather(SiteItf father) throws RemoteException {
		this.father = father;
	}


	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}

	
/*	
	@Override
	public void run(){
		while(true){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {e.printStackTrace();}
		}
	}
	
*/
	



}
